#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
extern char** environ;
int main()
{
    pid_t id = fork();
    if (id == 0)
    {
        printf("我是子进程，pid: %d\n", getpid());
        char *const argv[] = 
        {
            (char*)"ls",
            (char*)"-l",
            (char*)"--color",
            (char*)"-a",
            nullptr
        };
        char* const argv1[] = 
        {
            (char*)"other",
            nullptr
        };
        //(void)argv;
        char* const env[] = 
        {
            (char*)"hello1=BitAAAAA",
            nullptr
        };
        putenv((char*)"hello1=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        execve(argv1[0],argv1,environ);
        //execvpe("oother",ther",argv1,environ);
        //execle("./other","other",nullptr,environ);
        //execle("/usr/bin/ls","ls","-l","--color","-a",nullptr,env);
        //execvp("ls",argv);
        //execvp("./other",argv1);
        //execlp("ls","ls","-l","--color","-a",nullptr);
        //execlp("./other","other",nullptr);
        //execv("/usr/bin/ls",argv);
        //execl("./other","other",nullptr);
        //execl("/usr/bin/ls","ls","-l","--color","-a",nullptr);
        exit(100);
    }
    
    int status;
    pid_t w = waitpid(id,&status,0); //阻塞等待
    if (w > 0)
    {
        printf("等待子进程成功，id: %d, exit code: %d\n", w,WEXITSTATUS(status));
    }
    return 0;
}

